package com.vincent.controller.admin;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.service.RolePermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by vincent on 15-1-3.
 */
@Controller
@RequestMapping("/admin/rolepermission")
public class RolePermissionController {
    @Resource
    private RolePermissionService rolePermissionService;

    @RequestMapping(value = "/getRolePermissionsAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody
    Object getRolePermissions(@RequestParam Integer id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Map<String, Object> serviceMap = rolePermissionService.getRolePermissions(id);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),errorObj);
            return resultMap;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return resultMap;
    }

    @RequestMapping(value = "/grantRolePermissionsAjax",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody
    Object grantRolePermissions(@RequestParam Integer roleId,@RequestParam("perIds[]")Integer []perIds){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        Map<String, Object> serviceMap = rolePermissionService.grantRolePermissions(roleId,perIds);
        Object errorObj = ResultMapUtil.getErrorObj(serviceMap);
        if(errorObj!=null){
            resultMap.put(ResultMapKey.ERROR.getCode(),errorObj);
            return resultMap;
        }
        Object successObj = ResultMapUtil.getSuccessObj(serviceMap);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),successObj);
        return resultMap;
    }


}
